package com.example.deepschool.Controller.CourseContent;

import com.example.deepschool.Model.CourseContent;
import com.example.deepschool.Model.Response.ApiResponse;
import com.example.deepschool.enums.GlobalVariables_String;
import com.example.deepschool.service.composite.CompositeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.core.io.support.ResourceRegion;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Tag(name = "课程内容API")
@RestController("CourseContent")
@RequestMapping("/CourseContent")
public class Controller {
	
	// 用户令牌的全局变量
	private final String TOKEN = GlobalVariables_String.TOKEN.getValue();
	
	@Operation(summary = "添加课程内容",
	           description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`"
	)
	@PostMapping("/add")
	public ResponseEntity<Object> addCourseContent (@Validated @RequestBody CourseContent courseContent,
	                                                String JsonContent,
	                                                HttpServletRequest request) {
		String UUID = request.getHeader(TOKEN);
		CompositeService.courseContent.addCourseContent(courseContent, JsonContent, UUID);
		return ApiResponse.GetApiResponse().ok().body("添加成功.").build();
	}
	
	@Operation(summary = "更新课程内容",
	           description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`"
	)
	@PostMapping("/update")
	public ResponseEntity<Object> update (@Validated @RequestBody CourseContent courseContent,
	                                      String JsonContent,
	                                      HttpServletRequest request) {
		String UUID = request.getHeader(TOKEN);
		CompositeService.courseContent.update(courseContent, JsonContent, UUID);
		return ApiResponse.GetApiResponse().ok().body("更新成功.").build();
	}
	
	@Operation(summary = "删除课程内容",
	           description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`"
	)
	@DeleteMapping("/delete")
	public ResponseEntity<Object> delete (@Validated @RequestBody CourseContent courseContent,
	                                      HttpServletRequest request) {
		String UUID = request.getHeader(TOKEN);
		CompositeService.courseContent.delete(courseContent, UUID);
		return ApiResponse.GetApiResponse().ok().body("删除成功.").build();
	}
	
	@Operation(summary = "根据课程ID获取课程内容列表",
	           description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
	@GetMapping("/getCourseContentByCourseId")
	public ResponseEntity<Object> getCourseContentByCourseId (@RequestParam Long courseId,
	                                                          HttpServletRequest request) {
		String UUID = request.getHeader(TOKEN);
		return ApiResponse.GetApiResponse().ok()
				       .body(CompositeService.courseContent.getCourseContentByCourseId(courseId, UUID))
				       .build();
	}
	
	@Operation(summary = "根据课程内容ID获取一个章节的内容",
	           description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
	@GetMapping("/getCourseContentJsonData")
	public ResponseEntity<ResourceRegion> getCourseContentJsonData (Long courseId,
	                                                                String address,
	                                                                HttpServletRequest request) {
		String UUID = request.getHeader(TOKEN);
		
		return ApiResponse.GetApiResponse()
				       .binBody(CompositeService.courseContent.getCourseContentJsonData(courseId, address, UUID))
				       .onlineBuild();
	}
}
